package com.example.user.biz.controller;

import com.example.common.context.BaseContext;
import com.example.common.domain.Result;
import com.example.user.biz.domain.dto.AddAddressDTO;
import com.example.user.biz.domain.dto.AddressDTO;
import com.example.user.biz.domain.vo.AddressVO;
import com.example.user.biz.service.AddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user/address")
@Tag(name = "收货地址相关接口")
@RequiredArgsConstructor
public class AddressController {
    private final AddressService addressService;

    @Operation(summary = "添加收货地址")
    @PostMapping
    public Result<String> addAddress(@RequestBody @Validated AddAddressDTO addAddressDTO) {
        log.info("添加收货地址:{}", addAddressDTO);
        addressService.addAddress(addAddressDTO);
        return Result.success();
    }

    @Operation(summary = "删除收货地址")
    @DeleteMapping("/{id}")
    public Result<String> deleteAddress(@PathVariable Integer id) {
        log.info("删除收货地址:{}", id);
        addressService.deleteAddress(id);
        return Result.success();
    }

    /**
     * 查询当前用户地址列表
     *
     * @return
     */
    @Operation(summary = "查询当前用户地址列表")
    @GetMapping("/list")
    public Result<List<AddressVO>> findMyAddresses() {
        List<AddressVO> list = addressService.findMyAddresses(BaseContext.getCurrentId());
        return Result.success(list);
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @Operation(summary = "根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressVO> findById(@PathVariable Integer id) {
        log.info("查询{}地址:", id);
        AddressVO addressVO = addressService.findById(id);
        return Result.success(addressVO);
    }

    @Operation(summary = "修改收货地址")
    @PutMapping("/{id}")
    public Result<String> updateAddress(@PathVariable Integer id,@RequestBody @Validated AddressDTO AddressDTO) {
        AddressDTO.setId(id);
        log.info("修改收货地址:{}", AddressDTO);
        addressService.updateAddress(AddressDTO);
        return Result.success();
    }
}
